package com.acane.instagram.mapper;

import com.acane.instagram.pojo.dto.ChatDTO;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.springframework.stereotype.Repository;

import java.time.LocalDateTime;
import java.util.List;
@Repository
@Mapper
public interface ChatMessageMapper extends BaseMapper<ChatDTO> {

    @Select("SELECT * FROM chat_messages WHERE receiver = #{receiver} ORDER BY timestamp")
    List<ChatDTO> getMessagesByUser(@Param("receiver") String receiver);

    @Select("SELECT * FROM chat_messages WHERE (sender = #{sender} " +
            "AND receiver = #{receiver}) OR (sender = #{receiver} " +
            "AND receiver = #{sender}) ORDER BY timestamp DESC")
    List<ChatDTO> getHistory(@Param("sender") String sender, @Param("receiver") String receiver);


    @Select("SELECT * FROM chat_messages ORDER BY timestamp DESC LIMIT #{page}, #{size}")
    List<ChatDTO> getChatList(Integer page, Integer size);

    @Select({
            "<script>",
            "SELECT id, sender, receiver, content, media_type, timestamp",
            "FROM chat_messages",  // 修改表名
            "<where>",
            "   <if test='sender != null'> AND sender = #{sender} </if>",
            "   <if test='receiver != null'> AND receiver = #{receiver} </if>",
            "   <if test='content != null'> AND content LIKE CONCAT('%', #{content}, '%') </if>",
            "   <if test='startTime != null'> AND timestamp &gt;= #{startTime} </if>",
            "   <if test='endTime != null'> AND timestamp &lt;= #{endTime} </if>",
            "   <if test='mediaType != null'> AND media_type = #{mediaType} </if>", // 新增媒体类型条件
            "</where>",
            "ORDER BY timestamp DESC",
            "LIMIT #{offset}, #{size}",
            "</script>"
    })
    List<ChatDTO> selectChatList(
            @Param("sender") String sender,
            @Param("receiver") String receiver,
            @Param("content") String content,
            @Param("startTime") LocalDateTime startTime,
            @Param("endTime") LocalDateTime endTime,
            @Param("mediaType") String mediaType, // 新增参数
            @Param("offset") int offset,
            @Param("size") int size
    );

    @Select({
            "<script>",
            "SELECT COUNT(*) FROM chat_messages",  // 修改表名
            "<where>",
            "   <if test='sender != null'> AND sender = #{sender} </if>",
            "   <if test='receiver != null'> AND receiver = #{receiver} </if>",
            "   <if test='content != null'> AND content LIKE CONCAT('%', #{content}, '%') </if>",
            "   <if test='startTime != null'> AND timestamp &gt;= #{startTime} </if>",
            "   <if test='endTime != null'> AND timestamp &lt;= #{endTime} </if>",
            "   <if test='mediaType != null'> AND media_type = #{mediaType} </if>", // 新增媒体类型条件
            "</where>",
            "</script>"
    })
    int countChatList(
            @Param("sender") String sender,
            @Param("receiver") String receiver,
            @Param("content") String content,
            @Param("startTime") LocalDateTime startTime,
            @Param("endTime") LocalDateTime endTime,
            @Param("mediaType") String mediaType  // 新增参数
    );
}
